package Controller.Actions.Search;

import Controller.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Model.Beans.Results.*;
import Model.Beans.*;
import Model.QBE.*;

public class SearchStudentPerCourseAction implements Action {
    
    private String name;
    
    public SearchStudentPerCourseAction() 
    {
        this.name = "SearchStudentPerCourse";
    }
    
    public String getName()
    {
        return name;
    }
    
    public void perform(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        
        String id = request.getParameter("course_id");
        
        ScheduleModel smodel = new ScheduleModel();
        StudentModel stmodel = new StudentModel();
        CourseModel cmodel = new CourseModel();
        
        //Creamos objeto resultado (Course_Student)
        Course_Student result = new Course_Student();
        result.setCourse(cmodel.search_by_id(id));
        
        
        //Obtenemos todos los ids de estudiantes
        Schedule tmp = new Schedule();
        ScheduleList lists = new ScheduleList();
        tmp.setCourseId(Integer.parseInt(id));
        
        lists = smodel.search(tmp);

        //Iteramos la lista
        for(int i=0; i<lists.getSchedule().size(); i++)
        {
            int student_id = ((Schedule)lists.getSchedule().get(i)).getStudentId();
            result.addStudent(stmodel.search_by_id(Integer.toString(student_id)));
        }
        response.getWriter().write(result.toXML());
    }
}
